﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmDinhKhoanPhieuChiTien : Form
    {
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();

        private PhieuChiTien phieuchitien;
        public PhieuChiTien PhieuChiTien
        {
            set { this.phieuchitien = value; }
            get { return this.phieuchitien;}
        }

        public FrmDinhKhoanPhieuChiTien()
        {
            InitializeComponent();
        }

        private void FrmDinhKhoanPhieuChiTien_Load(object sender, EventArgs e)
        {

            dgvDinhKhoan.Columns["colSoTien"].ReadOnly = false;

            cboSoTK.DataSource = GetAllTaiKhoanKeToan();
            cboSoTK.DisplayMember = "MaSoTK";
            cboSoTK.ValueMember = "Id";

            txtSoChungTu.Text = phieuchitien.MaSo;
            txtMSKH.Text = phieuchitien.DoiTuong;
            txtNoiDung.Text = phieuchitien.LyDo;
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (from cn in GetAllChiNhanhCongTy()
                                                      where cn.Id == phieuchitien.IdChiNhanhCongTy
                                                      select cn).ToList();
            if (lstChiNhanhCongTy.Count > 0)
                txtChiNhanh.Text = lstChiNhanhCongTy[0].TenChiNhanh;
            AddRowToDGV();
            

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            if (dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString() == "Nợ")
            {
                MessageBox.Show("Định khoản " + dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString() + " phải chọn có !!!", "Thông báo");
                return;
            }

            //check 1121 || 1111 = "Co" != 1521 "No" and != 1331 "No"
            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString() == "Có" || dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString() == "")
                {
                    MessageBox.Show("Các định khoản khác " + dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString() + " phải chọn nợ !!!", "Thông báo");
                    return;
                }
            }

            long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[3].Value.ToString());
            if (sotien != phieuchitien.SoTien)
            {
                MessageBox.Show("Vui lòng điều chỉnh số tiền còn thiếu cho đúng !","Thông báo");
                return;
            }

            EditOrRemoveDinhKhoan(phieuchitien.MaSo);
            ResultObj resultObj = new ResultObj();

            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                {
                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = 0;
                    buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[3].Value.ToString());
                    resultObj = buttoanMgt.SaveNewButToan(buttoan);

                    buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[3].Value.ToString());
                    buttoan.GhiCo = 0;
                    resultObj = buttoanMgt.SaveNewButToan(buttoan);
                }

            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                this.Close();
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
        }

        private void dgvDinhKhoan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoan.Rows[e.RowIndex].Cells[0].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;

                dgvDinhKhoan.Rows[e.RowIndex].Cells[2].Value = tenTK;
                dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = 0;
            }
            //&& (dgvDinhKhoan.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "1121" || dgvDinhKhoan.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "1111")
            if (e.ColumnIndex == 3 && e.RowIndex > -1 )
            {
                if (dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            long tongBaBaMot = 0;
            for (int j = 1; j < dgvDinhKhoan.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoan.Rows[j].Cells[3].Value != null)
                {
                    tongBaBaMot += Convert.ToInt64(dgvDinhKhoan.Rows[j].Cells[3].Value.ToString());
                }
            }
            dgvDinhKhoan.Rows[0].Cells[3].Value = tongBaBaMot;
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;
            return listChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public void AddRowToDGV()
        {
            //add row tk "có 1121 | 1111"
            DataGridViewRow row = new DataGridViewRow();

            DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();
            TaiKhoanKeToan tkkt = new TaiKhoanKeToan();

            lstTaiKhoanKeToan = (from tk in GetAllTaiKhoanKeToan()
                                 where tk.MaSoTK.StartsWith("111")
                                 select tk).ToList();
            
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "1111")
                    tkkt = lstTaiKhoanKeToan[i];

            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Có";
            row.Cells.Add(cboCell);

            DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan.Rows.Add(row);

            dgvDinhKhoan.Rows[0].Cells[3].Value = 0;
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan.CurrentRow != null)
            {
                if (dgvDinhKhoan.CurrentRow.Index != (dgvDinhKhoan.Rows.Count - 1))
                {
                    dgvDinhKhoan.Rows.RemoveAt(dgvDinhKhoan.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[dgvDinhKhoan.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    dgvDinhKhoan.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoan_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan.ClearSelection();
                    dgvDinhKhoan.CurrentCell = dgvDinhKhoan.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoan.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }        
    }
}
